// pages/listDetail/index.js
import { get, post } from '../../utils/request.js'
import global from '../../utils/global.js'
import { starCount, updateStart} from '../../utils/util.js'
import { makePhoneCall, openLocation } from '../../utils/wxapi.js'
var WxParse = require('../../wxParse/wxParse.js');
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stars:"",
    currentTab:0,
    currentTab1:0,
    clikcurrentTab1:false,
    list1:["全部","男性","女性"],
    list2: ["全部", " 0~1000 ", "1001~3000", "3001~5000 ","5000 + "],
    smallTab: [[true], [true]],
    sellerId:"",
    sellerDetail:{},//商家详情\
    lat:"",
    lon:"",
    desc:0,
    page:1,
    dataType:0,
    orderWay:"ASC",
    listTT:[],
    height:"",
    isMore:[true,true,true]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    that = this
    that.setData({
      stars: starCount(3.5),
      listCur:that.data.list2,
      sellerId: options.id
    })
    that.getSeller(that.data.sellerId);
    that.getSellerProductList();
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          height: res.windowHeight / 2 - 50
        })
      }
    })
  },
  onShow:function(){
    that.setData({
      userInfo: wx.getStorageSync("useInfo")
    })
  },
// 获取商家列表
  getSeller:(id)=>{
    post(global.apiUrl.sellerDetail, {sellerId:id}, (res)=> {
      console.log(res.obj)
      let stars = []
      res.obj.stars = starCount(res.obj.commentScore)
      WxParse.wxParse('article', 'html', res.obj.buyInfo, that, 5)
      that.setData({
        sellerDetail: res.obj,
        lat: res.obj.lat,
        lon: res.obj.lon,
        name: res.obj.name,
        address: res.obj.address
      })
    })
  },
  // 获取商家列表
  getSellerProductList: ()=>{
    let param = {
      page:that.data.page++,
      sellerId: that.data.sellerId,
      dataType: that.data.dataType,
      orderWay: that.data.orderWay
    }
    post(global.apiUrl.sellerProductList, param, (res) => {
      console.log(res)
      if (!res.more) {
        wx.showToast({
          title: '已经加载完数据',
          icon: 'loading',
          duration: 1000
        })
        that.data.isMore[that.data.currentTab1] = false
        console.log(that.data.isMore)
        that.setData({
          isMore: that.data.isMore
        })
        return
      }
      let curArr = that.data.listTT.concat(res.obj)
      that.setData({
        listTT: curArr
      })
    })
  },
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '取消授权',
        success: function (res) { }
      })
    } else {
      that.getUserInfo(e.detail.encryptedData, e.detail.iv)
      console.log(e)
      that.goAppointment(e);
    }

  },
  // 获取用户信息
  getUserInfo(encryptedData, ivData) {
    // doLogin
    let param = {
      encryptedData,
      ivData,
      js_code: wx.getStorageSync('js_code')
    }
    console.log("什么情况")
    post(global.apiUrl.doLogin, param, function (res) {
      console.log(res.obj, "ceshiyixia")
      wx.setStorageSync("useInfo", res.obj)
    })
  },
  // 打开地图
  openMap: ()=>{
    // scale: scale,name: name,address: address
    openLocation(that.data.lat, that.data.lon, "", that.data.name, that.data.address )
  },
// 拨打电话
  openPhone:(e)=>{
    console.log(e.target.dataset.phone)
    makePhoneCall(e.target.dataset.phone)
  },


  
  //点击切换
  clickTab: function (e) {
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current, 
      })
    }
  },
  clickTab1: function (e) {
    that.setData({
      page:1,
      listTT:[],
      isMore: [true, true, true]
    })
    if (e.target.dataset.current1 == 2) {
      console.log(that.data.listTT)
      if (that.data.orderWay == "DESC") {
        that.setData({
          dataType: e.target.dataset.current1 * 1,
          orderWay: "ASC",
          currentTab1: e.target.dataset.current1,
        })
      }else{
        that.setData({
          dataType: e.target.dataset.current1 * 1,
          orderWay: "DESC",
          currentTab1: e.target.dataset.current1,
        })
      }
      
      that.getSellerProductList();
      return;
    }
    if (that.data.currentTab1 === e.target.dataset.current1) {    
      return false;
    } else {
      that.setData({
        currentTab1: e.target.dataset.current1,
        dataType: e.target.dataset.current1*1,
        // orderWay: that.data.orderWay
      })
    }
    that.getSellerProductList();
  },
  // // 点击弹出层
  // clickLayer:()=>{
  //   that.setData({
  //     currentTab1: 3,
  //     clikcurrentTab1: false
  //   })
  // },
  // // 小按钮切换
  // smallTab:(e)=>{
  //   that.data.smallTab[that.data.currentTab1] = [];
  //   that.data.smallTab[that.data.currentTab1][e.target.dataset.current1] = true;
  //   that.setData({
  //     smallTab: that.data.smallTab,
  //   })
  // },
  // 去产品列表
  goAppointment(e){
    console.log(e.currentTarget.dataset.id)
  wx.navigateTo({
    url: 'packageDetail/packageDetail?productId='+ e.currentTarget.dataset.id,
  })
  },
   /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    // 停止下拉动作
    wx.stopPullDownRefresh();

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  lower: function () {
    if (!that.data.isMore[that.data.currentTab1]) {
      return
    }
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    // 页数+1
    that.getSellerProductList();
  },
})